﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GUIServerConnectorAPI;
using EntityModel;


//Cum folosesti relatia many to many
//http://smehrozalam.wordpress.com/2010/06/29/entity-framework-queries-involving-many-to-many-relationship-tables/
//succes
namespace LoginForm
{
    public partial class MedicForm : Form
    {
        private LoginForm loginForm;
        private Guid sessionId;

        public MedicForm(LoginForm loginForm)
        {
            InitializeComponent();
            this.loginForm = loginForm;
        }

        public MedicForm()
        {
            // TODO: Complete member initialization
            InitializeComponent();
        }

        public void Show(Guid sessionId)
        {
            this.sessionId = sessionId;
            Show();
        }

        private void MedicForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            GUIServerConnector.getInstance().getAuthenticator().logout(sessionId);
            this.Hide();
            loginForm.Reset(this);
        }

        private void AddPacient_Click(object sender, EventArgs e)
        {
           
        }

        private void MedicForm_Load(object sender, EventArgs e)
        {
            List<Pacient> listaPacienti = GUIServerConnector.getInstance().getServerInterface().getPacientsData(sessionId);
            listView.BeginUpdate();
            foreach (Pacient pacient in listaPacienti)
            {
                ListViewItem item = new ListViewItem(pacient.Nume.ToString()+" "+pacient.Prenume.ToString());
                listView.Items.Add(item);            
            }
            listView.EndUpdate();
        }

        public void LoadPacientData(Pacient pacient)
        {
            // Pacient pacient = GUIServerConnector.getInstance().getServerInterface().getPacientData(sessionId);
            NumeLabel.Text = "Nume: " + pacient.Nume.ToString();
            PrenumeLabel.Text = "Prenume: " + pacient.Prenume.ToString();
            SexLabel.Text = "Sex: " + pacient.Sex.ToString();
            DataNasteriiLabel.Text = "Data Nasterii: " + pacient.Data_nasterii.ToShortDateString();
            CNPLabel.Text = "CNP: " + pacient.CNP.ToString();
            EmailLabel.Text = "Email: " + pacient.Email.ToString();
            idPacientLabel.Text = "idPacient: " + pacient.idPacient.ToString();
            TelefonLabel.Text = "Telefon: " + pacient.Telefon.ToString();
            RecomandariLabel.Text = "Recomandari: " + pacient.Recomandari_medicale.ToString();
            MasuriLabel.Text = "Masuri Fiziologice: " + pacient.Masurari_fiziologice.ToString();
            AlergiiLabel.Text = "Lista Alergii: " + pacient.Lista_alergi.ToString();
            IstoricLabel.Text = "Istoric Medical: " + pacient.Istoric_medical.ToString();
            //  idPacientVar = pacient.idPacient.ToString();

        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Pacient> listaPacienti = GUIServerConnector.getInstance().getServerInterface().getPacientsData(sessionId);

            foreach (Pacient pacient in listaPacienti)
            {
                if (listView.SelectedItems.Count > 0)
                {
                    foreach (ListViewItem selected in listView.SelectedItems)
                    {
                        string numePrenume = pacient.Nume + " " + pacient.Prenume;
                        if (selected.Text.Equals(numePrenume))
                        {
                            LoadPacientData(pacient);
                        }
                    }
                }
            }
        }

        private void listView_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
            e.NewWidth = listView.Columns[e.ColumnIndex].Width;
        }
    }
}
